package com.tfy.liquor.controller;

import com.tfy.liquor.bean.Result;
import com.tfy.liquor.service.RolloverRecordsService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
public class UploadController {
    @Autowired
    private RolloverRecordsService rolloverRecordsService;

    // 导入礼包转购物券记录
    @RequestMapping("/uploadRolloverRecordsFile")
    public Result fileUpload(
            @RequestParam("rolloverRecordsFile") MultipartFile file) {
        if (!file.isEmpty()) {
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = file.getOriginalFilename().indexOf(".");
            int last = file.getOriginalFilename().length();
            //获得文件后缀名
            String name = file.getOriginalFilename().substring(begin, last);
            //我这边需要的xlsx文件所以说我这边直接判断就是了
            if (name.endsWith(".xlsx")) {
                InputStream inputStream;
                Workbook workbook = null;//  创建工作表
                try {
                    inputStream = file.getInputStream();
                    workbook = WorkbookFactory.create(inputStream);
                    // 关闭输入流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (rolloverRecordsService.addRolloverRecords(workbook)) {
                    return new Result().ok("数据导入成功！");
                }
                return new Result().ok("数据导入失败！");
            } else {
                return new Result().ok("只能上传Excel文件！");
            }
        } else {
            return new Result().ok("不能上传空文件！");
        }
    }

    /**
     * 上传Excel文件
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/excel/upload")
    public String uploadExcel(@RequestParam("file") MultipartFile file) throws java.lang.Exception {
        if (null == file) {
            return "文件不能为空";
        }
        InputStream inputStream = file.getInputStream();


        Workbook wb;
        try {
            //  创建工作表
            wb = WorkbookFactory.create(inputStream);

            //开始解析
            Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

            int firstRowIndex = sheet.getFirstRowNum() + 1;   //第一行是列名，所以不读
            int lastRowIndex = sheet.getLastRowNum();
            System.out.println("firstRowIndex: " + firstRowIndex);
            System.out.println("lastRowIndex: " + lastRowIndex);

            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                System.out.println("rIndex: " + rIndex);
                Row row = sheet.getRow(rIndex);
                if (row != null) {
                    int firstCellIndex = row.getFirstCellNum();
                    int lastCellIndex = row.getLastCellNum();
                    for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                        Cell cell = row.getCell(cIndex);
                        if (cell != null) {
                            System.out.println(cell.toString());
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println(e);
            System.err.println("出错了！出现异常！");
            System.err.println(e.getMessage());
        } finally {
            // 关闭输入流
            inputStream.close();
        }

        return "上传成功";
    }
}
